package com.itheima.auth.handle;


import com.alibaba.fastjson.JSONObject;
import com.itheima.entry.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
										AuthenticationException exception) throws IOException, ServletException {
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		Result result = new Result(false,exception.getMessage());
		Map param = new HashMap<>();
		param.put("flag",result.isFlag());
		param.put("data",result.getData());
		param.put("message",result.getMessage());
		response.getWriter().write(new JSONObject(param).toString());
	}

}
